﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LDTB.Service;

namespace LDTB
{
    public partial class EditWard : Form
    {
        LDTBDataDataContext ds = DataService.DB;
        int id;

        public EditWard()
        {
            InitializeComponent();
        }

        private void EditWard_Load(object sender, EventArgs e)
        {
            var ward = (from w in ds.Wards
                        where w.ID == id
                        select w).FirstOrDefault();

            this.txtWardName.Text = ward.WardName;
        }

        public EditWard(int id_)
        {
            InitializeComponent();
            id = id_;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string wardName = txtWardName.Text.Trim();


            if (wardName == string.Empty)
            {
                MessageBox.Show("Nhập tên huyện");
            }
            else
            {

                //insert to db

                var ward = (from w in ds.Wards
                            where w.ID == id
                            select w).FirstOrDefault();

                ward.WardName = wardName;


                ds.SubmitChanges();

                if (ward.ID != 0)
                {
                    MessageBox.Show("Đã sửa thành công");
                }
                else
                {

                    MessageBox.Show("Sửa thất bại");

                }

            }
        }
    }
}
